package com.example.studykotlin.kotlin_actual_combat.MyKotlin

import android.os.Bundle
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.example.studykotlin.*
import com.example.studykotlin.databinding.ActivityMyKotlinBinding
import com.example.studykotlin.kotlin_actual_combat.BaseActivity.BaseActivity
import com.example.studykotlin.kotlin_actual_combat.Bean.ItemSingleClickMessage
import com.example.studykotlin.kotlin_actual_combat.annotation.EventMode
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

@Route(path = Path.myKotlin)
class MyKotlinActivity : BaseActivity() {
    private lateinit var binding: ActivityMyKotlinBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        EventBus.getDefault().register(this)
        binding = ActivityMyKotlinBinding.inflate(layoutInflater)
        setContentView(binding.root)
        initView()
        initData()
    }

    private fun initView() {
        binding.recycler.layoutManager = getLinearLayoutManager()
        binding.recycler.addItemDecoration(getItemDecoration())
    }

    private fun initData() {
        val listData = mutableListOf(
            resources.getStringArray(R.array.MyKotlin)[0]
        )
        listAdapter = ListAdapter(listData)
        listAdapter.eventMode = EventMode.MY_KOTLIN
        binding.recycler.adapter = listAdapter
    }

    override fun onDestroy() {
        EventBus.getDefault().unregister(this)
        super.onDestroy()
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun handleItemSingleClickMessage(message: ItemSingleClickMessage) {
        if (message.eventMode != EventMode.MY_KOTLIN) return
        when(message.position) {
            0 -> {
                ARouter.getInstance().build(Path.myView).navigation()
            }
        }

    }
}